<template>
  <div class="change-password-wrapper">
    <el-card class="box-card change-password-card">
      <h3 class="title">首次登录—修改密码 & 完善个人信息</h3>
      <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="100px"
          class="change-password-form"
      >
        <el-form-item label="姓名" prop="adName">
          <el-input
              v-model="form.adName"
              placeholder="请输入姓名"
          />
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input
              v-model="form.phone"
              placeholder="请输入电话"
          />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
              v-model="form.newPassword"
              type="password"
              placeholder="请输入新密码"
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="form.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
          />
        </el-form-item>

        <el-form-item class="button-item">
          <el-button
              type="primary"
              size="medium"
              @click="onSubmit"
              class="submit-button"
          >
            保存
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getCurrentUser, updateAdmin } from '@/api/apiList/admin'

const router = useRouter()
const formRef = ref(null)
const form = reactive({
  adId: null,
  newPassword: '',
  confirmPassword: '',
  adName: '',
  phone: '',
  adAccount:''
})
const rules = {
  newPassword: [{ required: true, min: 6, message: '至少 6 位', trigger: 'blur' }],
  confirmPassword: [{
    validator: (rule, val) => val === form.newPassword,
    message: '两次密码必须一致', trigger: 'blur'
  }],
  adName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入电话', trigger: 'blur' }]
}

// 初始填充
onMounted(async () => {
  const admin = await getCurrentUser()
  form.adId = admin.adId
  form.adName = admin.adName
  form.phone = admin.phone
  form.adAccount = admin.phone
})

async function onSubmit() {
  await formRef.value.validate(async valid => {
    if (!valid) return
    try {
      const adminUpdate = {
        adId: form.adId,
        adPassword: form.newPassword,
        adName: form.adName,
        phone: form.phone
      }
      await updateAdmin(adminUpdate)
      ElMessage.success('资料更新成功，请使用新密码重新登录')
      localStorage.removeItem('token')
      localStorage.removeItem('adminName')
      localStorage.removeItem('role')
      router.replace({name: 'Login'})
    } catch {
      ElMessage.error('更新失败')
    }
  })
}
</script>

<style scoped>
.change-password-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 40px;
}

.change-password-card {
  width: 400px;
  padding: 20px;
}

.title {
  text-align: center;
  margin-bottom:40px;
  color: #333;
}

.change-password-form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.el-form-item {
  width: 100%;
  display: flex;
  justify-content: center;
}

.el-form-item[label] .el-form-item__content {
  width: 260px;
}

.el-form-item[label] .el-form-item__label {
  text-align: right;
  padding-right: 10px;
}

.button-item {
  margin-top: 20px;
  text-align: center;
}

.submit-button {
  width: 100px;
  margin-left:40px;
}
</style>
